import { FETCH_HOME_DATA, CHANNEL_LOADMORE, SWITCH_CHANNEL, INIT_ARTICLES, INIT_HOME_ARTICLES } from '../actions/home';
import { FETCH_ARTICLE_DATA } from '../actions/article';
import img from '../images/main.png';
const INITIAL_STATE = {  };

export default function (state = INITIAL_STATE, action) {
    switch (action.type) {
        case FETCH_HOME_DATA:
            let portals = [];
            let brandings = [];
            let channels = [];
            let hots = [];
            if (action.payload.data) {
                if (action.payload.data.portals && action.payload.data.portals.length > 0) {
                    portals = action.payload.data.portals;
                }
                if (action.payload.data.brandings && action.payload.data.brandings.length > 0) {
                    brandings = action.payload.data.brandings;
                }
                if (action.payload.data.channels && action.payload.data.channels.length > 0) {
                    channels = action.payload.data.channels;
                }
                if (action.payload.data.Hots && action.payload.data.Hots.length > 0) {
                    hots = action.payload.data.Hots;
                }
            }
            /*channels.push({
                "ID": -1,
                "Portal": null,
                "Title": "全部内容",
                "ChannelImageUrl": img,
                "ChannelIconUrl": img
            });*/
            let channelsStr = JSON.stringify(channels);
            sessionStorage.channels = channelsStr;
            return { 
                ...state,
                portals: portals,
                brandings: brandings,
                channels: channels
            };
        case SWITCH_CHANNEL:
        case CHANNEL_LOADMORE:
            let articles = [];
            if (action.payload.data) {
                if (action.payload.data.articles && action.payload.data.articles.length > 0) {
                    articles = action.payload.data.articles;
                }
            }
            let page = action.payload.data.page || 1;
            let pageCount = action.payload.data.pageCount || 1;
            return { channels: state.channels, articles: articles, page:page  ,pageCount:pageCount};
        case FETCH_ARTICLE_DATA:
            if (action.payload && action.payload.data && action.payload.data.articles && action.payload.data.articles.length > 0) {
                if (state.articles) {
                    state.articles = state.articles.concat(action.payload.data.articles);
                } else {
                    state.articles = action.payload.data.articles;
                }
                state.page = action.payload.data.page
            }
            return { ...state };
        case INIT_ARTICLES:
            return { ...state, articles: []};
        case INIT_HOME_ARTICLES:
            
            return { ...state, articles: action.payload.articles, page: action.payload.page, pageCount: action.payload.pageCount};
        default:
            return state;
    }
}

